package cn.duckerkj.springbootguide.controller;

import javax.validation.Valid;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.duckerkj.springbootguide.bean.TestBean;
import cn.duckerkj.springbootguide.bean.TestTwoBean;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
public class HellowController {
	
	@Autowired RedisTemplate redisTemplate;
	
	@RequestMapping(value="/set", method=RequestMethod.GET)
	@ApiOperation(value="测试", notes="所有请求")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
	public TestBean test(/*@RequestParam("name") String name ,@RequestParam("createTime") String createTime , */String id,@Valid TestBean bean ,BindingResult bindingResult) {
		/*log.info("name:{}" ,name);
		log.info("createTime:{}" ,createTime);*/
		log.info("createTime:{}" ,bean.getCreateTime());
		if(bindingResult.hasErrors()) {
			log.error(bindingResult.getFieldError().getDefaultMessage());
		}
		TestTwoBean testTwoBean = new TestTwoBean();
		testTwoBean.setCreateTime(new DateTime());
		testTwoBean.setName("testTwoBean");
		testTwoBean.setSex("female女");
		TestBean testBean = new TestBean();
		testBean.setCreateTime(new DateTime());
		testBean.setName("testBean");
		testBean.setSex("male男2");
		testBean.setTestTwoBean(testTwoBean);
		redisTemplate.opsForValue().set("测试test"+id, testBean);
		TestBean res = (TestBean)redisTemplate.opsForValue().get("测试test");
		return res;
	}
	
	@RequestMapping(value="/get", method=RequestMethod.GET)
	public TestBean get(String id) {
		TestBean res = (TestBean)redisTemplate.opsForValue().get("测试test"+id);
		return res;
	}
}
